﻿namespace PugLib.Collections
{
	using System;

	/// <summary>
	/// Interface for object-level validation container.
	/// Contains a value that can be validated.
	/// </summary>
	/// <typeparam name="T">Contained value type</typeparam>
	public interface IValidatable<T>
	{
		/// <summary>
		/// Contained value
		/// </summary>
		T Value { get; set; }

		/// <summary>
		/// Validate this object
		/// </summary>
		/// <returns></returns>
		bool IsValid();
	}

	/// <summary>
	/// Timeout validation container.
	/// Contained value is invalidated when the timeout expires.
	/// </summary>
	/// <typeparam name="T">Contained value type</typeparam>
	public class Timeoutable<T> : IValidatable<T>
	{
		public Timeoutable(T value, DateTime expirationTime)
		{
			Value = value;
			ExpirationTime = expirationTime;
		}

		public Timeoutable(T value, TimeSpan timeoutDuration)
		{
			Value = value;
			ExpirationTime = DateTime.Now + timeoutDuration;
		}

		public DateTime ExpirationTime { get; set; }

		#region IValidatable<T> Members
		public T Value { get; set; }

		public bool IsValid()
		{
			return DateTime.Now < ExpirationTime;
		}
		#endregion

		public override int GetHashCode()
		{
			return Value.GetHashCode() + ExpirationTime.GetHashCode();
		}
	}
}